/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.api.composante;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.event.EventListenerList;

import fr.univ_tln.inf9.exaplanning.api.composante.event.GroupAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.composante.event.GroupRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.composante.event.ListGroupListener;
import fr.univ_tln.inf9.exaplanning.api.db.AbstractEntity;
import fr.univ_tln.inf9.exaplanning.api.db.EntityInfo;
import fr.univ_tln.inf9.exaplanning.api.db.SessionManager;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialListener;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialRemovedToListEvent;

/**
 * @author emilien
 *
 */
@EntityInfo(	key = "name",
                variables = { "component_name" }, 
                methods = { "getName", "getComponent_name" },
                table = "training_path",
                rows = { "name_training_path", "name_component" },
                collections = { Group.class }) 
                
public class TrainingPath extends AbstractEntity {
        
        private final String name;
        
        private final String component_name;
        
        private List<String> groups;
        private EventListenerList listeners;
        
        /**
         * Constructeur pour créer une nouvelle filière
         * @param name le nom de la filière
         * @param component le nom de la composante auquelle est rattachée la filière
         * @throws SecurityException
         * @throws IOException
         * @throws NoSuchFieldException
         * @throws SQLException
         */
        public TrainingPath(String name, Component component) throws SecurityException, IOException, NoSuchFieldException, SQLException {
        		super(true);
                this.name = name;
                this.component_name = component.getName();
        }
        
        /**
         * Le constructeur interne appelé uniquement par {@link fr.univ_tln.inf9.exaplanning.api.TrainingPath#findByKey(String key) TrainingPath.findByKey(String Key)}
         * @param name Le nom de la filière
         * @param component le nom de la composante auquelle est rattachée la filière
         * @throws SecurityException
         * @throws IOException
         * @throws NoSuchFieldException
         * @throws SQLException
         */
        private TrainingPath(String name, String component) throws SecurityException, IOException, NoSuchFieldException, SQLException {
        		super(false);
        		this.name = name;
        		this.component_name = component;
        }

        public String getName() {
                return name;
        }
        
        public List<String> getGroups() {
                return groups;
        }
        
        public void addGroup(String group) {
                groups.add(group);
                fireAddGroupToList(group);
        }
        
        public void removeGroup(String group) {
                groups.remove(group);
                fireRemoveGroupToList(group);
        }

        public String getComponent_name() {
                return component_name;
        }
        
        private void setListGroups(List<String> groups) {
        		this.groups = groups;
        		//fireListGroupsChanged();
        }
        
        
        
        public void addGroupListener(ListGroupListener listener){
    		listeners.add(ListGroupListener.class, listener);
    	}
    	
    	public void removeListGroupListener(ListGroupListener l){
    		 listeners.remove(ListGroupListener.class, l);
    	}
 
    	
    	private void fireAddGroupToList(String elementAdded) {
    		ListGroupListener[] listenerList = (ListGroupListener[])listeners.getListeners(ListGroupListener.class);
    		
    		for(ListGroupListener listener : listenerList){
    			listener.GroupAddedToList(new GroupAddedToListEvent(this, elementAdded));
    		}
    		
    	}
    	
    	
    private void fireRemoveGroupToList(String elementRemoved) {
    		ListGroupListener[] listenerList = (ListGroupListener[])listeners.getListeners(ListGroupListener.class);
    		
    		for(ListGroupListener listener : listenerList){
    			listener.GroupRemovedToList(new GroupRemovedToListEvent(this, elementRemoved));
    		}
    		
    	}
        /**
         * Retourne une instance de filière depuis la base de donnée à partir
         * de la clef key qui est le nom de la filière
         * @param key nom de la filière
         * @return objet de type TrainingPath dont le nom est key
         * @throws SQLException
         * @throws SecurityException
         * @throws IOException
         * @throws NoSuchFieldException
         */
        public static TrainingPath findByKey(String key) throws SQLException, SecurityException, IOException, NoSuchFieldException {
        	List<List<String>> data = SessionManager.getInstance().getData(TrainingPath.class, key);
        	
        	List<String> variables = data.get(0);
        	TrainingPath result = new TrainingPath(variables.get(0), variables.get(1));
        	result.setListGroups(data.get(1));
        	
        	return result;
        }
}
